﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EFSQLiteOneToManay
{
    public class TestContext : DbContext
    {
        private static TestContext _instance;

        public static TestContext Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new TestContext();
                }
                return _instance;
            }
        }

        private string _connectionString;

        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_connectionString))
                {
                    _connectionString = ConfigurationManager.ConnectionStrings["testConn"].ConnectionString;
                }
                return _connectionString;
            }
            set
            {
                _connectionString = value;
            }
        }

        public TestContext() : base("DefaultConnection")
            //: base("name=testConn")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<TestContext, Configuration>());
            //_connectionString = ConfigurationManager.ConnectionStrings["testConn"].ConnectionString;
        }
        public TestContext(string connectionString)
            : base(connectionString)
        {

        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ///DomainMapping  所在的程序集一定要写对，因为目前在当前项目所以是采用的当前正在运行的程序集 如果你的mapping在单独的项目中 记得要加载对应的assembly
            ///这是重点
            ///

            //var ss = Assembly.GetExecutingAssembly().GetTypes();
            //foreach (var mm in ss)
            //{
            //    try
            //    {

            //        if (mm.BaseType != null && mm.BaseType.BaseType != null && mm.BaseType.BaseType.IsGenericType && mm.BaseType.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>))
            //        {
            //            Console.WriteLine(mm);
            //        }
            //    }
            //    catch (Exception x)
            //    {
            //        Console.WriteLine(x.Message);
            //    }
            //}

            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Configurations.AddFromAssembly(typeof(TestContext).Assembly);

            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
          .Where(type => !String.IsNullOrEmpty(type.Namespace))
          .Where(type => type.BaseType != null
          && type.BaseType.BaseType != null
          && type.BaseType.BaseType.IsGenericType
          && type.BaseType.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)
          ).ToList();
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);
        }


    }
}
